import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import { getFaqData, updateFaqData, addFaqData } from "@/api/raycloud";
// 组件
import FormCom from "@c/form/Index";


class FaqModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            faq_id: "",
            formConfig: {
                url: "faqList",
                editKey: "",
                initValue: {
                    id: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "SelectComponent",
                    label: "问题分类", 
                    mode: "tags",
                    url: "faqTypeList",
                    method: "get",
                    name: "type",
                    propsKey: {
                        label: "type",
                        value: "type"
                    },
                    required: true,
                    style: { width: '100%' },
                    placeholder: "请选择分类,可自定义输入"
                },
                { 
                    type: "Input",
                    label: "关键字", 
                    name: "keyword", 
                    required: true, 
                    style: { width: '100%' },
                    placeholder:"多个关键字以,分割"
                },
                { 
                    type: "Radio",
                    label: "格式", 
                    name: "answer_type", 
                    required: true,
                    options: [
                      { label: "markdown", value: "markdown" },
                      { label: "文本", value: "text" },
                      { label: "链接", value: "link" },
                      { label: "卡片", value: "action_card" },
                      { label: "图片", value: "img" },
                    ],
                    style: { width: '100%' },
                },
                { 
                    type: "inputTextArea",
                    label: "回答", 
                    name: "answer", 
                    maxlength: 1000,
                    required: true, 
                    style: { width: '100%' }
                },
            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        this.setState({
            isModalVisible: params.status,
            faq_id: params.faq_id
        }, () => {
            this.getDetailed();
        })
    }

    /** slb详情 */
    getDetailed = () => {
        if(!this.state.faq_id) { return false; }
        getFaqData({id: this.state.faq_id}).then(response => {
            const data = response.data.data.items[0];
            console.log(data)
            this.setState({
                formConfig: {
                    setFieldValue: data
                }
            })
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.state.faq_id ? this.handlerFormEdit(value) : this.handlerFormAdd(value);
    }
    // 添加 提交表单
    handlerFormAdd= (value) => {
        const requestData = value;
        console.log(requestData)
        addFaqData(requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }
    // 编辑 提交表单
    handlerFormEdit= (value) => {
        console.log(value)
        const requestData = value;
        const id = this.state.faq_id;
        updateFaqData(id, requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }

    render(){
        return (
            <Modal title="Q&A详情" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom onRef={this.onFormRef} onBlur={this.onBlurEvent} formItem={this.state.formItem} formLayout={this.state.formLayout} formConfig={this.state.formConfig} submit={this.submit}>
                </FormCom>
            </Modal>
        )
    }
}

export default FaqModal;